{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd04c36a90a135e4aa4256adc3692917ccc380701b533777f3da43dc8bc5264a29c",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 第一次作业第5题\n",
    "作者：赵飞\n",
    "学号：202018019427078\n",
    "日期：2021-04-18"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "数组Z中最接近z=5.1的数是： 5\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "def find_most_approximate(A, z):\n",
    "    '''\n",
    "    找出数组A中与z最接近的数，即abs(a-z)最小的a\n",
    "    '''\n",
    "    b = np.abs(A-z)\n",
    "    idx = np.argmin(b)\n",
    "    return A.flatten()[idx]\n",
    "\n",
    "Z = np.array([[0,1,2,3],[4,5,6,7]])\n",
    "z = 5.1\n",
    "ma = find_most_approximate(Z, z)\n",
    "print(\"数组Z中最接近z=5.1的数是：\", ma)"
   ]
  }
 ]
}